pixeldungeonfandomcom-20200225-history
Some notes to source code
= Some notes to source code = I did this meanwhile I was studying source. Maybe it will help someone, although it probably contains a lot of errors in english and in source code understanding and it is undoubtedly far from complete. Globals Game The main Activity class is PixelDungeon (Class hiearchy: Game -> PixelDungeon). Its ancestor Game contains methods for updating display, listening touch and keypress events etc. Some interesting properties (of class Game) * Game.scene contains GameScene object responsible for drawing on the screen. * Game create(), resume(), pause(), finish() Dungeon Class Dungeon contains many “global” methods and moreover some helpers used in many places. Some interesting properties * Dungeon.level keeps current level (class Level) * Dungeon.hero is link to player object (class Hero) * init() performs initialization of some main game structures such as challenges, statistics, quests, scrolls, badges, hero etc. * newLevel(), loadGame(), saveGame(), loadLevel(), saveLevel(), switchLevel() Notes For saving are used methods storeInBundle(), restoreFromBundle() of interface Bundlable implemented in many classes. Savefiles are stored in directory %UserProfile%/Saved Games on Windows system. Another important structure is for example Dungeon.visible array (field of view of the hero) Level generator Level Level is represented as descendant of class Level. Each particular level type has its own class. Class hiearchy: Level -> RegularLevel -> particular level class *Package: pixeldungeon.levels.** Actual level can be found in Dungeon.level variable. The map of the level is stored in flat array Level.map. It contains integer constants such as Terrain.DOOR or Terrain.WALL You can access each square (cell) of the map as Dunegeon.level.mappos. In other words - any cell in the map can be addressed with simple integer value pos. Some interesting properties * mobs, heaps, blobs, plants - keeps monsters, items etc. * passable, flammable, secret, visited, mapped - boolean arrays same size as level.map are used for check if cell is passable, flammable etc. * create(), initRooms(), joinRooms(), createItems(), createMobs(), placeDoors(), placeTraps() - level generation stuff * press(), mobPress() - called everytime when hero or mob step on some position (cell) in the level In RegularLevel * rooms - list of rooms (class Room) * roomEntrace, roomExit - link to entrace and exit room * secretDoors, specials New level is created with method Level.create() Here and in its descendant RegularLevel are called method such as initRooms(), createItems(), createMobs() etc. We will cover some of them later. Some interesting methods * set(pos, terrain) - set terrain (such as Terrain.GRASS) at pos position of the level. You must call GameScene.updateMap(pos) then. * drop( Item item, int cell) - drop an item at cell position * createItems() : Drop items randomly in the level. Always will drop Heap - it is class which can contain one or more items Level generation It’s only my understanding of basic principe and maybe I am completly wrong… At first it setup rectangle same size as level and split it recursively in initRooms(). We got something like this: +----+--+-----+ | | | | | +-++-+---+ | | | | | +----+-+--+---+ * Next it will add rooms into level.rooms variable ( Room is descendant of rectangle class Rect) * Some of that rooms will be used as real dungeon rooms, some will be used for building tunnels, some are just unused. * Then it find neighbours and save references in room.neigbours for each room. * Finnaly it builds graph of connections between rooms and store references to connected rooms in room.connected HashMap The real placement of map cells is done by Painter classes. Method paint() goes through each room and call Painter.paint() method. Each room type has its own Painter class such as LibraryPainter, GardenPainter, EntracePainter or TunnelPainter. Room Class hiearchy: Rect -> Room Some interesting properties * left, top, right, bottom - rectangle coordinates * intersect(Rect other), union(Point p), inside(Point p)… intersection, union, test if point is inside rectangle… * neighbours, connected - lists of neighbours and connected rooms * type - Room.Type - such as STANDARD, TUNNEL, SHOP, GARDEN… * paint() - paint room cells into the level map * random() return random cell inside room as level.map pos coordinate * Room.Door - door class (class hiearchy: Point -> Door) Notes * Level type (SewerLevel, SewerBossLevel, CavesLevel, …) is chosen in Dungeon.newLevel() by Dungeon.depth variable * Level is populated with mobs using class Bestiary - Mob mob = Bestiary.mob( Dungeon.depth ); see also mobClass() method. * For choosing items to be placed into level is used class items.Generator: weapon = (Weapon)Generator.random( Generator.Category.WEAPON ); Drawing on the screen GameScene Class hiearchy: Gizmo -> Group -> Scene -> PixelScene -> GameScene *Package: pixeldungeon.scenes.** GameScene is scene graph - a tree of nodes containing visual components such as terrains, mobs, effects, gases… Any visual element is descendant od Gizmo - base class containing a few generic properties such as parent, update(), remove(). Group is Gizmo which can contain another Gizmos and GameScene is Group too. Actual scene can be obtained as PixelDungeon.scene(). Some interesting properties * create() - add all necessary visual elements to GameScene (tiles, mobs, items,…) * update() - draws all Gizmos in GameScene.members * add() Added element to GameScene * It reads dungeon content from Dungeon.level Example of adding tiles, heaps(items), water etc: //in GameScene.create(): terrain = new Group(); add( terrain ); GameScene.tiles = new DungeonTilemap(); terrain.add( tiles ); ... terrain.add( water ); add( heaps ); DungeonTilemap is responsible for drawing tiles of level.map Class hiearchy: Gizmo -> Visual -> TileMap -> DungeonTilemap TileMap.draw() draws TileMap.data using updateVertices() Water drawing Under whole level is SkinnedBlock of water texture. Through each tile with alpha-transparency is visible flowing water. Water tiles are at the end of tiles0.png asset. CharSprite, MobSprite, ItemSprite, HeroSprite Class hiearchy: Gizmo -> Visual -> Image -> MovieClip -> CharSprite *Package: pixeldungeon.sprites.** Any character, mob, item, hero has visual representation in class …Sprite. Visual representation of any character (mob, hero) is stored in sprite variable of class Char. Each monster has his own class defining animations etc., for example RatSprite or BatSprite. Class hiearchy: CharSprite -> MobSprite -> RatSprite All animation frames (for RatSprite) are stored in Assets.RAT file which is rat.png in this case. Dungeon habitants - mobs Mob is any living (mobile) being in dungeon, except Hero, which has his own class. Both, mobs and hero share common ancestor Char (character) Class hiearchy: Actor -> Char -> Mob -> particular mob class *Package: pixeldungeon.actors.mobs.** AiState Models of behaviour of the mobs (living beings) are defined in classes Mob.Passive, Mob.Sleeping, Mob.Wandering, Mob.Hunting, Mob.Fleeing - all implementing interface Mob.AiState Attack If mob is hunting, he checks if he can see enemy and can attack it. //in Mob.Hunting.act(): if (enemyInFOV && canAttack( enemy )) { doAttack(enemy) } In doAttack() method it calls char.attack(enemy), play attack animation, if fight is visible and spend(attackDelay()) time with the attack. in Char.attack(enemy) method: * Compute if enemy is hitted by hit() method, uses: attackSkills(), defenseSkills() * Compute effective damage by damageRoll(), attackProc(), defenseProc() * Wound enemy by Char.damage() - basically: hp -= dmg Some interesting properties (Char class) * attack(), attackProc(), attackSkill(), damage() - fight methods * damageRoll(), defenseProc(), defenseSkill(), hit() - fight methods * move(), die() - another character activities * flying, invisible, paralysed, rooted - boolean stats flags * buffs, name, sprite - buffs hashset, character name, character sprite * pos, HP - position, health Some interesting properties (Mob class) * dropLoot(), getCloser(), getFurther() - more character activies * alerted, enemySeen, hostile - more boolean stats * enemy, target, loot - links to mob enemy, target and loot - if he have one * Fleeing, Hunting, Passive, Sleeping… - models of behaviour - classes containing method act() Mob class contains code which makes no sense for player controlled Char -> Hero. Buffs Buffs are temporary conditions of some character (Mob, Hero), such as Burning, Invisibility, Levitation, Sleep etc. Every Buff has own particular class with most important method act() which is called for each turn and perform effects on character for duration time period. Class hiearchy: Actor -> Buff -> particular buff class //Adding a condition to some mob: Buff.affect(mob, Burning.class, Burning.DURATION); Hero Class hiearchy: Actor -> Char -> Hero *Package: pixeldungeon.actors.hero.** Some interesting properties * heroClass - class of the player such as HeroClass.ROGUE or HeroClass.WARRIOR * STR, lvl, exp - strength, level, experience * belongings - Player belongings (class Belongings) * enemy - Actual enemy (target of attacks) * attackSkill(), defenseSkill(), damageRoll(), attackProc(), defenseProc(), … - fight methods * handle(), act(), actPickUp(), actMove(), actOpenChest(),… player activity (pick up, move…) * move(), getCloser(), add(buff), die(), search() - moving, adding some condition (Buff) etc. Handling player activity Game is calling Dungeon.hero.handle() while processing of player input. Here instance of HeroAction is build, which describes the activity and it is set to hero.curAction variable. Action is processed in method act(), where methods such as actMove(), actAttack() etc. are called. Belongings Storage for hero belongings (hero.belongings) Some interesting properties * backpack - Player inventory - class Bag * weapon, armor, ring1, ring2 Player equipment - classes KindOfWeapon, Armor, Ring from pixeldungeon.items package * getItem(), getKey() - Get item or key by type User interface * Game windows are defined in pixeldungeon.windows namespace as Wnd… classes. * UI elements such as GameLog, SimpleButton or QuickSlot are in namespace pixeldungeon.ui * Scenes, such as AboutScene, StartScene, GameScene can be found in pixeldungeon.scenes Category:Modding Category:Code